<!-- xml version="1.0" encoding="utf-8" -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
	<title>Latin Comparisons</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<link href="flashcard.css" rel="stylesheet" type="text/css">
</head>

<?php

try {
	$nouns = array();
	$db = new PDO('sqlite:Latin.db3');
	$q = $db->query('SELECT DISTINCT word FROM comparisons;');
	$tmp = $q->fetchAll();
	$tmp2 = array_rand($tmp, 5);
	$wds = array();
	foreach($tmp2 as $n) {
		$wds[] = "word='".$tmp[$n]['word']."'";
	}

	$words = array();
	$q = $db->query("SELECT meaning, form FROM comparisons WHERE ".implode(" OR ", $wds).";");
	foreach( $q->fetchAll() as $row) {
		$words[] = array($row['meaning'], $row['form']);
	}

	shuffle($words);
	
	$db = NULL;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

?>
<script type="text/javascript">
<?php
print "var words = new Array(".count($words).");\n";
for($i = 0; $i < count($words); $i++) {
	$tmp = implode("', '", $words[$i]);
	$tmp = preg_replace("/\s*<i>(.*?)<\/i>(.*)$/", " $2 <span class=\"wl\">$1</span>", $tmp);
	print "words[$i] = ['$tmp'];\n";
	
}
?>

var shown = 0;

function updateVoc() {
	var i = Math.floor(shown / 2);

	var j = shown % 2;

	if (j == 0) {
		document.getElementById('v3').innerHTML = words[i][0];
		document.getElementById('v1').innerHTML = "";
	} else {
		document.getElementById('v3').innerHTML = words[i][0];
		document.getElementById('v1').innerHTML = words[i][1];
	}
}

function next() {
	shown++;
	if (shown > words.length * 2 - 1) {
		shown = 0;
		alert('All done!');
	}
	updateVoc();
}

function prev() {
	shown--;
	if (shown < 0) {
		shown = words.length * 2 - 1;
		alert('All done!');
	}
	updateVoc();
}

</script>

<body onload="updateVoc()">

<h1 style="text-align:center; color: #60B0C0;">Latin Comparisons</h1>

<div class="center">
<div id="blank" class="v2">&nbsp;</div>
<div id="v3" class="v3"></div>
<div id="blank" class="v2">&nbsp;</div>
<div id="v1" class="v1"></div>


<a href="#" class="button" id="btnprev" onclick="prev()" accesskey="p">&lt;&lt;</a>
<a href="#" class="button" id="button" onclick="location.reload();">new</a>
<a href="#" class="button" id="btnnext" onclick="next()" accesskey="n">&gt;&gt;</a>


</div>

</body>
</html>
